<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name="copyright" content=
"Copyright (c) IBM Corporation and others 2000, 2011. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css" />
<title>Java model</title>
<link rel="stylesheet" type="text/css" href="../book.css" />
</head>
<body>
<h2>Java model</h2>
<p>The Java model is the set of classes that model the objects associated with creating, editing,
and building a Java program. The Java model classes are defined in <b><a href=
"../reference/api/org/eclipse/jdt/core/package-summary.html">org.eclipse.jdt.core</a></b>.&nbsp;

These classes implement Java specific behavior for resources and further decompose Java resources
into model elements.</p>
<h3>Java elements</h3>
<p>The package <a href=
"../reference/api/org/eclipse/jdt/core/package-summary.html"><b>org.eclipse.jdt.core</b></a>
defines the classes that model the elements that compose a Java program. The JDT uses an in-memory
object model to represent the structure of a Java program. This structure is derived from the
project's class path. The model is hierarchical. Elements of a program can be decomposed into child
elements.</p>
<p>Manipulating Java elements is similar to manipulating resource objects.&nbsp; When you work with
a Java element, you are actually working with a <b>handle</b> to some underlying model
object.&nbsp; You must use the <b>exists()</b> protocol to determine whether the element is
actually present in the workspace.&nbsp;</p>

<p>The following table summarizes the different kinds of Java elements.</p>
<table border="1">
<tbody>
<tr>
<th rowspan="1" colspan="1">Element</th>
<th rowspan="1" colspan="1">Description</th>
</tr>
<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IJavaModel.html"><b>IJavaModel</b></a></td>
<td>Represents the root Java element, corresponding to the workspace. The parent of all projects
with the Java nature. It also gives you access to the projects without the java nature.</td>
</tr>
<tr>

<td><a href="../reference/api/org/eclipse/jdt/core/IJavaProject.html"><b>IJavaProject</b></a></td>
<td>Represents a Java project in the workspace. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IJavaModel.html"><b>IJavaModel</b></a>)</td>
</tr>

<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IModuleDescription.html"><b>IModuleDescription</b></a></td>
<td>Represents a Java module descriptor. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IJavaProject.html"><b>IJavaProject</b></a>)</td>
</tr>

<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/IPackageFragmentRoot.html"><b>IPackageFragmentRoot</b></a></td>
<td>Represents a set of package fragments, and maps the fragments to an underlying resource which
is either a folder, JAR, or ZIP file. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IJavaProject.html"><b>IJavaProject</b></a>)</td>
</tr>
<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/IPackageFragment.html"><b>IPackageFragment</b></a></td>

<td>Represents the portion of the workspace that corresponds to an entire package, or a portion of
the package. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IPackageFragmentRoot.html"><b>IPackageFragmentRoot</b></a>
)</td>
</tr>
<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/ICompilationUnit.html"><b>ICompilationUnit</b></a></td>
<td>Represents a Java source (<b>.java</b>) file. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IPackageFragment.html"><b>IPackageFragment</b></a> )</td>
</tr>

<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/IPackageDeclaration.html"><b>IPackageDeclaration</b></a></td>
<td>Represents a package declaration in a compilation unit. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/ICompilationUnit.html"><b>ICompilationUnit</b></a> )</td>
</tr>
<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/IImportContainer.html"><b>IImportContainer</b></a></td>
<td>Represents the collection of package import declarations in a compilation unit. (Child of
<a href="../reference/api/org/eclipse/jdt/core/ICompilationUnit.html"><b>ICompilationUnit</b></a>
)</td>

</tr>
<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/IImportDeclaration.html"><b>IImportDeclaration</b></a></td>
<td>Represents a single package import declaration. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IImportContainer.html"><b>IImportContainer</b></a> )</td>
</tr>
<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IType.html"><b>IType</b></a></td>
<td>Represents either a source type inside a compilation unit, or a binary type inside a class
file.</td>
</tr>
<tr>

<td><a href="../reference/api/org/eclipse/jdt/core/IField.html"><b>IField</b></a></td>
<td>Represents a field inside a type. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IType.html"><b>IType</b></a> )</td>
</tr>
<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IMethod.html"><b>IMethod</b></a></td>
<td>Represents a method or constructor inside a type. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IType.html"><b>IType</b></a> )</td>
</tr>

<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IInitializer.html"><b>IInitializer</b></a></td>
<td>Represents a static or instance initializer inside a type. (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IType.html"><b>IType</b></a> )</td>
</tr>
<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IClassFile.html"><b>IClassFile</b></a></td>
<td>Represents a compiled (binary) type.&nbsp; (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IPackageFragment.html"><b>IPackageFragment</b></a> )</td>

</tr>

<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IModularClassFile.html"><b>IModularClassFile</b></a></td>
<td>Represents the class file of a module description ("module-info.class").&nbsp; (Child of <a href=
"../reference/api/org/eclipse/jdt/core/IPackageFragment.html"><b>IPackageFragment</b></a> )</td>
</tr>

<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/ITypeParameter.html"><b>ITypeParameter</b></a></td>
<td>Represents a type parameter.&nbsp; (Not a child of any Java element, it is obtained using
<a href=
"../reference/api/org/eclipse/jdt/core/IType.html#getTypeParameter(java.lang.String)"><b>IType.getTypeParameter(String)</b></a>
or <a href=
"../reference/api/org/eclipse/jdt/core/IMethod.html#getTypeParameter(java.lang.String)"><b>IMethod.getTypeParameter(String)</b></a>)</td>
</tr>
<tr>
<td><a href=
"../reference/api/org/eclipse/jdt/core/ILocalVariable.html"><b>ILocalVariable</b></a></td>

<td>Represents a local variable in a method or an initializer.&nbsp; (Not a child of any Java
element, it is obtained using <a href=
"../reference/api/org/eclipse/jdt/core/ICodeAssist.html#codeSelect(int,%20int)"><b>ICodeAssist.codeSelect(int,
int)</b></a>)</td>
</tr>
<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IAnnotation.html"><b>IAnnotation</b></a></td>
<td>Represents a Java 5 annotation. (Not a child of any Java element, it is obtained using <a href=
"../reference/api/org/eclipse/jdt/core/IAnnotatable.html#getAnnotation(java.lang.String)"><b>IAnnotatable.getAnnotation(String)</b></a>
or <a href=
"../reference/api/org/eclipse/jdt/core/IAnnotatable.html#getAnnotations()"><b>IAnnotatable.getAnnotations()</b></a>)</td>

</tr>
<tr>
<td><a href="../reference/api/org/eclipse/jdt/core/IAnnotatable.html"><b>IAnnotatable</b></a></td>
<td>Represents a type, a field, a method, a local variable, or a package declaration that can be
annotated with one or several <a href=
"../reference/api/org/eclipse/jdt/core/IAnnotation.html"><b>IAnnotation</b></a>s.</td>
</tr>
</tbody>
</table>
<p>All Java elements support the <a href=
"../reference/api/org/eclipse/jdt/core/IJavaElement.html"><b>IJavaElement</b></a> interface.</p>
<p>Some of the elements are shown in the Packages view.&nbsp; These elements implement the

<b><a href="../reference/api/org/eclipse/jdt/core/IOpenable.html">IOpenable</a></b> interface,
since they must be opened before they can be navigated. The figure below shows how these elements
are represented in the Packages view.</p>
<p><img src="images/openables.png" border="0" alt=
"Packages View showing elements implementing the IOpenable interface" /></p>
<p>The Java elements that implement <b><a href=
"../reference/api/org/eclipse/jdt/core/IOpenable.html">IOpenable</a></b> are created primarily from
information found in the underlying resource files.&nbsp; The same elements are represented
generically in the resource navigator view.</p>
<p><img src="images/javanavigator.png" border="0" alt=
"Resource Navigator showing elements implementing the IOpenable interface" /></p>
<p>Since 3.3, <b><a href="../reference/api/org/eclipse/jdt/core/IOpenable.html">IOpenable</a></b>s
that are the root of <b><a href="../reference/api/org/eclipse/jdt/core/IType.html">IType</a></b>s
implement <b><a href="../reference/api/org/eclipse/jdt/core/ITypeRoot.html">ITypeRoot</a></b> .
These are <b><a href=
"../reference/api/org/eclipse/jdt/core/ICompilationUnit.html">ICompilationUnit</a></b> and

<b><a href="../reference/api/org/eclipse/jdt/core/IClassFile.html">IClassFile</a></b>.</p>
<p>Other elements correspond to the items that make up a Java compilation unit. The figure below
shows a Java compilation unit and a content outliner that displays the source elements in the
compilation unit.</p>
<img src="images/sourceelements.png" border="0" alt=
"An editor and a content outliner illustrating the relation between corresponding source elements" />
<p>These elements implement the <a href=
"../reference/api/org/eclipse/jdt/core/ISourceReference.html"><b>ISourceReference</b></a>
interface, since they can provide corresponding source code. (As these elements are selected in the
content outliner, their corresponding source code is shown in the Java editor).</p>
<h3>Java elements and their resources</h3>
<p>Many of the Java elements correspond to generic resources in the workspace.&nbsp; When you want
to create Java elements from a generic resource the class <a href=
"../reference/api/org/eclipse/jdt/core/JavaCore.html"><b>JavaCore</b></a> is the best starting
point. The following code snippet shows how to get Java elements from their corresponding
resources.</p>

<pre class="color1">
    
    private void createJavaElementsFrom(IProject myProject, IFolder myFolder, IFile myFile) {
        IJavaProject myJavaProject= JavaCore.create(myProject);
        if (myJavaProject == null)
            // the project is not configured for Java (has no Java nature)
            return;
            
        // get a package fragment or package fragment root
        IJavaElement myPackageFragment= JavaCore.create(myFolder);
        
        // get a .java (compilation unit), .class (class file), or
        // .jar (package fragment root)
        IJavaElement myJavaFile = JavaCore.create(myFile);
    }
</pre>
<p>Once you have a Java element, you can use the JDT API to traverse and query the model.&nbsp; You
may also query the non-Java resources contained in a Java element.&nbsp;</p>
<pre class="color1">
    private void createJavaElementsFrom(IProject myProject, IFolder myFolder, IFile myFile) throws JavaModelException {
        ...
        // get the non Java resources contained in my project.
        Object[] nonJavaChildren = myJavaProject.getNonJavaResources();
        ...
    }
</pre>
<h3>Java projects</h3>
<p>When you create a Java project from a simple project, <a href=
"../reference/api/org/eclipse/jdt/core/JavaCore.html"><b>JavaCore</b></a> will check to see if the
project is configured with the Java nature.&nbsp; The JDT plug-in uses a project nature to
designate a project as having Java behavior.&nbsp; This nature
(<b>org.eclipse.jdt.core.JavaCore#NATURE_ID</b> ) is assigned to a project when the "New Java
project" wizard creates a project.&nbsp; If the Java nature is not configured on a project,

<a href="../reference/api/org/eclipse/jdt/core/JavaCore.html"><b>JavaCore</b></a> will return null
when asked to create the project.</p>
<p><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html"><b>JavaCore</b></a> is also used
to maintain the Java class path, including locations for finding source code and libraries, and
locations for generating output binary (<b>.class</b>) files.</p>
<p>What are the unique characteristics of Java projects? They record their classpath in a
"<b>.classpath</b>" file and add the Java incremental project builder to the project's build
spec.&nbsp; Otherwise, they are just regular projects and can be configured with other natures (and
other incremental builders) by plug-ins. Plug-ins that want to configure projects with Java
behavior in addition to their own behavior typically use the <b><a href=
"../reference/api/org/eclipse/jdt/ui/wizards/NewJavaProjectWizardPage.html">NewJavaProjectWizardPage</a></b>

to assign the Java nature to the project in addition to their own custom natures or behavior.</p>
<p><a href="../reference/api/org/eclipse/jdt/core/IJavaModel.html"><b>IJavaModel</b></a> can be
considered the parent of all projects in the workspace that have the Java project nature (and
therefore can be treated as an <a href=
"../reference/api/org/eclipse/jdt/core/IJavaProject.html"><b>IJavaProject</b></a>).</p>
</body>
</html>
